package com.css.zfzx.sjcj.modules.qhdatatemplate.repository.impl;

import com.css.zfzx.sjcj.modules.qhdatatemplate.repository.QhDataTemplateNativeRepository;
import com.css.zfzx.sjcj.modules.qhdatatemplate.repository.entity.QhDataTemplateEntity;
import com.css.zfzx.sjcj.modules.qhdatatemplate.viewobjects.QhDataTemplateQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @author tao
 * @date 2020-11-19
 */
@Repository
public class QhDataTemplateNativeRepositoryImpl implements QhDataTemplateNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(QhDataTemplateNativeRepositoryImpl.class);


    @Override
    public Page<QhDataTemplateEntity> queryQhDataTemplates(QhDataTemplateQueryParams queryParams, int curPage, int pageSize) {
        if(PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            queryParams.setIsValid("1");
        }
        StringBuilder sql = new StringBuilder("select * from qh_data_template ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getTemplateName())) {
            whereSql.append(" and template_name like :templateName");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateUser())) {
            whereSql.append(" and create_user like :createUser");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            whereSql.append(" and is_valid = :isValid");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhDataTemplateEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from qh_data_template ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getTemplateName())) {
            query.setParameter("templateName", "%" + queryParams.getTemplateName() + "%");
            countQuery.setParameter("templateName", "%" + queryParams.getTemplateName() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCreateUser())) {
            query.setParameter("createUser", "%" + queryParams.getCreateUser() + "%");
            countQuery.setParameter("createUser", "%" + queryParams.getCreateUser() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            query.setParameter("isValid", queryParams.getIsValid());
            countQuery.setParameter("isValid", queryParams.getIsValid());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<QhDataTemplateEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<QhDataTemplateEntity> qhDataTemplatePage = new PageImpl<>(list, pageable, count.longValue());
        return qhDataTemplatePage;
    }
}
